package jp.co.sharp.android.parents.kidsguard.thread;

import android.util.Log;

import com.android.bluetooth.parentble.ParentBleTransfer;

import org.json.JSONObject;

import jp.co.sharp.android.parents.kidsguard.contants.Constants;
import jp.co.sharp.android.parents.kidsguard.contants.PageSerialNumConstants;
import jp.co.sharp.android.parents.kidsguard.contants.RequestMethodConstants;
import jp.co.sharp.android.parents.kidsguard.entity.KidInfoEntity;

public class BusinessThread extends Thread {

    private static final String TAG = "TAG";

    private int mViewProcess = 0;
    private ParentBleTransfer mParentBleTransfer;
    private KidInfoEntity mKidInfoEntity;
    private JSONObject mJsonObj;
    private int mPageSerialNum;
    /**
     *
     * @param pViewProcess 0: is using, 1: exit , 1: getInfo , 2: setInfo, 3 :exit
     * @param pParentBleTransfer
     * @param pKidInfoEntity (Selected KidInfo)
     */
    public BusinessThread(int pViewProcess, ParentBleTransfer pParentBleTransfer, KidInfoEntity pKidInfoEntity){
        this(pViewProcess,pParentBleTransfer,pKidInfoEntity,null,0);
    }
    /**
     *
     * @param pViewProcess 0: is using, 1: exit , 1: getInfo , 2: setInfo, 3 :exit
     * @param pParentBleTransfer
     * @param pKidInfoEntity (Selected KidInfo)
     * @param pPageSerialNum PageSerialNumConstants
     */
    public BusinessThread(int pViewProcess, int pPageSerialNum, ParentBleTransfer pParentBleTransfer, KidInfoEntity pKidInfoEntity){
        this(pViewProcess,pParentBleTransfer,pKidInfoEntity,null,pPageSerialNum);
    }
    /**
     *
     * @param pParentBleTransfer
     * @param pKidInfoEntity (Selected KidInfo)
     */
    public BusinessThread(JSONObject pJSONObject, ParentBleTransfer pParentBleTransfer, KidInfoEntity pKidInfoEntity){
        this(Constants.SEND_KID_GET_INFO,pParentBleTransfer,pKidInfoEntity,pJSONObject,0);
    }
    /**
     *
     * @param pViewProcess 0: is using, 1: exit , 1: getInfo , 2: setInfo, 3 :exit
     * @param pParentBleTransfer
     * @param pKidInfoEntity (Selected KidInfo)
     * @param pJSONObject JSON(Get, Set)
     * @param pPageSerialNum PageSerialNumConstants
     */
    public BusinessThread(int pViewProcess, ParentBleTransfer pParentBleTransfer, KidInfoEntity pKidInfoEntity, JSONObject pJSONObject, int pPageSerialNum) {
        this.mViewProcess = pViewProcess;
        this.mParentBleTransfer = pParentBleTransfer;
        this.mKidInfoEntity = pKidInfoEntity;
        this.mJsonObj = pJSONObject;
        this.mPageSerialNum = pPageSerialNum;
    }

    @Override
    public void run() {
        businessProcess();
    }

    private void businessProcess() {
        switch(mViewProcess) {
            case Constants.SEND_KID_IS_USING:
                sendIsUsingToKid();
                break;
            case Constants.SEND_KID_ENTER:
                sendEnterToKid();
                break;
            case Constants.SEND_KID_GET_INFO:
                sendGetToKid();
                break;
            case Constants.SEND_KID_SET_INFO:
                sendSetToKid();
                break;
            case Constants.SEND_KID_EXIT:
                sendExitToKid();
                break;
        }
    }

    /**
     * パージォウク
     */
    private void sendIsUsingToKid() {
        try {
            JSONObject jsonObject = new JSONObject();
            jsonObject.put("method", RequestMethodConstants.METHOD_GET_OPERATING_STATE);
            JSONObject jsonData = new JSONObject();
            jsonData.put("state", 0);
            jsonObject.put("data", jsonData);

            mParentBleTransfer.writeLeData(mKidInfoEntity.getMacAddress(), jsonObject.toString());
        } catch (Exception e) {
            Log.e(TAG,e.getMessage());
        }
    }

    private void sendEnterToKid() {
        try {
            JSONObject jsonObject = new JSONObject();
            jsonObject.put("method", RequestMethodConstants.METHOD_ENTER);
            JSONObject jsonData = new JSONObject();
            jsonData.put("page", mPageSerialNum);
            jsonObject.put("data", jsonData);

            mParentBleTransfer.writeLeData(mKidInfoEntity.getMacAddress(), jsonObject.toString());
        } catch (Exception e) {
            Log.e(TAG,e.getMessage());
        }
    }

    private void sendGetToKid() {
        try {
            mParentBleTransfer.writeLeData(mKidInfoEntity.getMacAddress(), mJsonObj.toString());
        } catch (Exception e) {
            Log.e(TAG,e.getMessage());
        }
    }

    private void sendSetToKid() {
        try {
            mParentBleTransfer.writeLeData(mKidInfoEntity.getMacAddress(), mJsonObj.toString());
        } catch (Exception e) {
            Log.e(TAG,e.getMessage());
        }
    }

    private void sendExitToKid() {
        try {
            JSONObject jsonObject = new JSONObject();
            jsonObject.put("method", RequestMethodConstants.METHOD_EXIT);
            JSONObject jsonData = new JSONObject();
            jsonData.put("page", mPageSerialNum);
            jsonObject.put("data", jsonData);

            mParentBleTransfer.writeLeData(mKidInfoEntity.getMacAddress(), jsonObject.toString());
        } catch (Exception e) {
            Log.e(TAG,e.getMessage());
        }
    }
}
